package top.carrotguo.weibo.adapter;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.ui.base.BaseActivity;
import top.carrotguo.weibo.ui.base.ImageActivity;
import top.carrotguo.weibo.ui.home.CommentActivity;
import top.carrotguo.weibo.ui.home.HomeDetailActivity;
import top.carrotguo.weibo.utils.DateUtils;
import top.carrotguo.weibo.utils.StringUtils;
import top.carrotguo.weibo.widget.WrapHeightGridView;

import static top.carrotguo.weibo.ui.home.CommentActivity.ID;
import static top.carrotguo.weibo.ui.home.HomeDetailActivity.BLOG_DATA;
import static top.carrotguo.weibo.ui.home.HomeDetailActivity.SCROLL;

public class StatusAdapter extends RecyclerView.Adapter<StatusAdapter.StatusViewHolder> {

    private ArrayList<HomeTimeLine.UsersBean> datas;
    private Context mContext;
    private ControllClickListener controllClickListener;
    private ItemClickListener itemClickListener;

    public StatusAdapter(Context context,ArrayList<HomeTimeLine.UsersBean> datas){
        this.mContext = context;
        this.datas = datas;
    }


    @NonNull
    @Override
    public StatusViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.item_status,viewGroup,false);
        return new StatusViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StatusViewHolder statusViewHolder, int i) {
        //用户名
        String userName = datas.get(i).getUser().getName();
        statusViewHolder.tvName.setText(userName+"");

        //来自
        String res = datas.get(i).getSource();
        Pattern pattern = Pattern.compile("<[^>]+>",Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(res);
        res = matcher.replaceAll("");
        statusViewHolder.tvRes.setText(res+"");
        statusViewHolder.tvFromText.setVisibility(TextUtils.isEmpty(res)?View.GONE:View.VISIBLE);

        //头像
        String headUrl = datas.get(i).getUser().getProfile_image_url();
        Glide.with(mContext).load(headUrl).into(statusViewHolder.ivHead);

        //设置正文内容
        statusViewHolder.tvText.setText(StringUtils.getContentText(mContext,statusViewHolder.tvText,datas.get(i).getText()));

        //设置发布时间
        statusViewHolder.tvCreateAt.setText(DateUtils.getShortTime(datas.get(i).getCreated_at()));

        //设置九宫格
        ArrayList<HomeTimeLine.UsersBean.Pic> pics = datas.get(i).getPicUrls();
        if (pics != null && pics.size()>1) {
            //显示九宫格
            statusViewHolder.gvPics.setVisibility(View.VISIBLE);
            statusViewHolder.ivPic.setVisibility(View.GONE);
            StatusGridImgsAdapter imageAdapter = new StatusGridImgsAdapter(mContext,pics);
            statusViewHolder.gvPics.setAdapter(imageAdapter);
        } else if (pics != null && pics.size() == 1) {
            //显示单张图片
            statusViewHolder.gvPics.setVisibility(View.GONE);
            statusViewHolder.ivPic.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                 .load(datas.get(i).getPicUrls().get(0).getNormal_pic())
                 .into(statusViewHolder.ivPic);
            //statusViewHolder.ivPic.measure(0,0);
            statusViewHolder.ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //显示单张图片
                    ArrayList<HomeTimeLine.UsersBean.Pic> pic = new ArrayList<>();
                    pic.add(datas.get(i).getPicUrls().get(0));
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(ImageActivity.IMAGES,pic);
                    bundle.putInt(ImageActivity.CUR_INDEX,0);
                    ((BaseActivity)mContext).readyGo(ImageActivity.class,bundle);
                }
            });
        } else {
            //隐藏九宫格
            statusViewHolder.gvPics.setVisibility(View.GONE);
            statusViewHolder.ivPic.setVisibility(View.GONE);
        }

        //设置转发
        HomeTimeLine.UsersBean retweet = datas.get(i).getRetweeted_status();
        if (retweet != null) {
            //设置转发内容和用户
            statusViewHolder.llRetweet.setVisibility(View.VISIBLE);

            statusViewHolder.tvRetweetContent
                    .setText(StringUtils.getContentText(mContext,statusViewHolder.tvRetweetContent,"@"+retweet.getUser().getName()+":"+retweet.getText()));

            //设置转发图片
            ArrayList<HomeTimeLine.UsersBean.Pic> retweetPics = retweet.getPicUrls();
            if (retweetPics != null && retweetPics.size()>1) {
                //显示多张图片效果
                statusViewHolder.ivRetweetPic.setVisibility(View.GONE);
                statusViewHolder.wgvRetweetPics.setVisibility(View.VISIBLE);
                StatusGridImgsAdapter adapter = new StatusGridImgsAdapter(mContext,retweetPics);
                statusViewHolder.wgvRetweetPics.setAdapter(adapter);
            } else if (retweetPics != null && retweetPics.size() == 1) {
                //显示单张图片效果
                statusViewHolder.wgvRetweetPics.setVisibility(View.GONE);
                statusViewHolder.ivRetweetPic.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                     .load(retweetPics.get(0).getNormal_pic())
                     .into(statusViewHolder.ivRetweetPic);
                statusViewHolder.ivRetweetPic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //显示单张图片
                        ArrayList<HomeTimeLine.UsersBean.Pic> pic = new ArrayList<>();
                        pic.add(retweetPics.get(0));
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(ImageActivity.IMAGES,pic);
                        bundle.putInt(ImageActivity.CUR_INDEX,0);
                        ((BaseActivity)mContext).readyGo(ImageActivity.class,bundle);
                    }
                });
            } else {
                //隐藏图片
                statusViewHolder.ivRetweetPic.setVisibility(View.GONE);
                statusViewHolder.wgvRetweetPics.setVisibility(View.GONE);
            }

        } else {
            statusViewHolder.llRetweet.setVisibility(View.GONE);
        }

        //设置转发按钮、评论按钮、点赞按钮
        int repostCount = datas.get(i).getReposts_count();
        statusViewHolder.tvToolRetweet.setText(repostCount>0?repostCount+"":"转发");
        int commentCount = datas.get(i).getComments_count();
        statusViewHolder.tvToolComment.setText(commentCount>0?commentCount+"":"评论");
        int attitudeCount = datas.get(i).getAttitudes_count();
        statusViewHolder.tvToolGood.setText(attitudeCount>0?attitudeCount+"":"点赞");

        final int position = i;
        //设置点击监听
        statusViewHolder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.click(position);
                }
            }
        });

        statusViewHolder.rlComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controllClickListener != null) {
                    //controllClickListener.clickComment(position);
                    if (datas.get(position).getComments_count()==0) {
                        //没有评论，则跳转评论页面
                        Bundle bundle = new Bundle();
                        bundle.putString(ID,datas.get(position).getId());
                        ((BaseActivity)mContext).readyGo(CommentActivity.class);
                    } else {
                        //有评论，则跳转评论列表页面
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(BLOG_DATA,datas.get(position));
                        bundle.putBoolean(SCROLL,true);
                        ((BaseActivity)mContext).readyGo(HomeDetailActivity.class,bundle);
                    }
                }
            }
        });

        statusViewHolder.rlRetweet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controllClickListener != null) {
                    controllClickListener.clickRetweet(position);
                }
            }
        });
        statusViewHolder.rlGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controllClickListener != null) {
                    controllClickListener.clickLike(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas==null ? 0:datas.size();
    }

    public static class StatusViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.root)
        LinearLayout llRoot;
        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_res)
        TextView tvRes;
        @BindView(R.id.gv_pic)
        WrapHeightGridView gvPics;
        @BindView(R.id.tv_text)
        TextView tvText;
        @BindView(R.id.tv_fromtext)
        TextView tvFromText;
        @BindView(R.id.iv_pic)
        ImageView ivPic;
        @BindView(R.id.include_retweet)
        View llRetweet;
        @BindView(R.id.tv_retweet_name_and_content)
        TextView tvRetweetContent;
        @BindView(R.id.iv_retween_pic)
        ImageView ivRetweetPic;
        @BindView(R.id.wgv_retween_pics)
        WrapHeightGridView wgvRetweetPics;
        @BindView(R.id.rl_tool_retweet)
        RelativeLayout rlRetweet;
        @BindView(R.id.rl_tool_comment)
        RelativeLayout rlComment;
        @BindView(R.id.rl_tool_good)
        RelativeLayout rlGood;
        @BindView(R.id.iv_tool_retween)
        ImageView ivToolRetween;
        @BindView(R.id.iv_tool_comment)
        ImageView ivToolComment;
        @BindView(R.id.iv_tool_good)
        ImageView ivToolGood;
        @BindView(R.id.tv_tool_retweet)
        TextView tvToolRetweet;
        @BindView(R.id.tv_tool_comment)
        TextView tvToolComment;
        @BindView(R.id.tv_tool_good)
        TextView tvToolGood;
        @BindView(R.id.tv_create_at)
        TextView tvCreateAt;

        public StatusViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }

    public void setControllClickListener(ControllClickListener controllClickListener){
        this.controllClickListener = controllClickListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    /**
     * 点赞、评论、转发的点击监听
     */
    public interface ControllClickListener{
        void clickLike(int position);
        void clickComment(int position);
        void clickRetweet(int position);
    }

    /**
     * 微博的点击监听
     */
    public interface ItemClickListener{
        void click(int position);
    }

}
